package serverController;

import implementations.GamePanelImpl;
import implementations.PlayerImpl;
import implementations.PointImpl;
import interfaces.IGamePanel;
import interfaces.IPlayer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

import clientController.Client;

public class Server extends Thread
{
	private boolean yourTurn;
	private int status = 0;
	private int svStat = 0;
	private int id = -1;
	private IPlayer player;
	private boolean opponentRdy;
	private IGamePanel panel;

	// private PlayerImpl player2;

	public Server(int id, IPlayer pPlayer,IGamePanel panel)
	{
		this.panel=panel;
		player = pPlayer;
		this.id = id;
		setYourTurn(false);
	}

	@Override
	public void run()
	{
		// TODO Auto-generated method stub
		super.run();
		try
		{
			startServer();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void startServer() throws IOException
	{
		IPlayer pl = (PlayerImpl) player;
		int portNumber;
		if (pl.getType() == 1)
		{
			portNumber = 81;
			yourTurn = true;
		} else
		{
			portNumber = 82;
			svStat = 1;
		}
		if (id == -1)
		{
			Random rd = new Random();
			id = rd.nextInt(1234);
		}
		System.out.println("Server:id:" + id);
		System.out.println("Creating server socket on port " + portNumber);
		ServerSocket serverSocket = new ServerSocket(portNumber);

		while (true)
		{
			// System.out.println("status 1");
			Socket socket = serverSocket.accept();

			System.out.println("Server:a client connected");

			OutputStream os = socket.getOutputStream();
			PrintWriter pw = new PrintWriter(os, true);
			// pw.println("What's you name?");

			BufferedReader br = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));
			int port = 0;

			String ip = "";
			String str = br.readLine();
			String str2 = br.readLine();

			// String auxId=id+"";
			// String auxStr=str.substring(auxId.length(),str.length());
			if (str2.compareTo("-2")==0)
			{
				opponentRdy = true;
			} else
			{
				// String str3="";
				System.out.println("Stats" + svStat);
				if (svStat == 0)
				{

					// str3=br.readLine();

					String auxStr = id + "";
					ip = br.readLine();

					// if (svStat == 0)
					port = Integer.parseInt(str2);
					port++;
				}

				System.out.println("Server:Message from client:" + str + " "
						+ str2 + " " + ip);
				if (svStat == 0)
				{
					int id1 = -1;
					try
					{
						id1 = Integer.parseInt(str);
					} catch (NumberFormatException e)
					{
						System.out.println("id not a number");
					}
					if (id1 != -1)
					{
						if (id1 == id)
						{

							System.out.println("Server:Create client2");
							Client.createClient(id, ip, port, player);
							svStat = 1;
							pw.println("Hello, " + str);
							pw.close();
							br.close();
							socket.close();

							// System.out.println("Just said hello to:" + str);
						} else
						{
							pw.println("Wrong id!!!");
							pw.close();
							br.close();
							socket.close();
							System.out.println("else1!!");
						}
					} else
					{
						pw.println("Wrong id!NAN!!");
						pw.close();
						br.close();
						socket.close();
						System.out.println("else2!!");
					}
				} else if (svStat == 1)
				{
					System.out
							.println("Server:placement time!You can not attack!"); // System.out.println("NU!");
					pw.println("-1");
					pw.close();
					br.close();
					socket.close();

				} else
				{
					if (str.compareTo(id + "") == 0)
					{
						System.out.println("Attacking=>>" + str2);
						int indexOf = str2.indexOf(';');
						
						int x=Integer.parseInt(str2.substring(0, indexOf));
						int y=Integer.parseInt(str2.substring(indexOf+1));
						System.out.println("x: "+x+" y: "+y);
						boolean resp=player.deffend(new PointImpl(x, y));
						if(resp)
						{
							pw.println("true");
							boolean END=player.isStillFighting();
							if(!END)
							{
								System.out.println("You are dead!");
							}
								
						}
						else 
							pw.println("false");
							panel.paintMyButtons(player.getMyBoard().getGrid());
							pw.close();
							br.close();
							socket.close();
							//Client.repaint(panel);
							this.setYourTurn(true);
							System.out.println("else3!!");
						
					} else
						System.out.println("NU!");
					this.setYourTurn(true);
					
				}
			}
		}
	}

	public int getStatus()
	{
		return status;
	}

	public int getSvStat()
	{
		return svStat;
	}

	public void setSvStat(int svStat)
	{
		this.svStat = svStat;
	}

	public boolean isYourTurn()
	{
		return yourTurn;
	}

	public void setYourTurn(boolean yourTurn)
	{
		this.yourTurn = yourTurn;
	}

	public boolean isOpponentRdy()
	{
		return opponentRdy;
	}

	public void setOpponentRdy(boolean opponentRdy)
	{
		this.opponentRdy = opponentRdy;
	}

}
